package com.ruoyi.project.module.propertyConstraint.controller;

import java.util.List;

import com.ruoyi.project.module.propertyConstraint.processor.model.PropConstraintVO;
import com.ruoyi.project.module.propertyConstraint.processor.scan.PropConstraintScan;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.propertyConstraint.domain.PropertyConstraint;
import com.ruoyi.project.module.propertyConstraint.service.IPropertyConstraintService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 配置属性约束 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-11-12
 */
@Controller
@RequestMapping("/module/propertyConstraint")
public class PropertyConstraintController extends BaseController
{
    private String prefix = "module/propertyConstraint";

	@Autowired
	PropConstraintScan propConstraintScan;

	@Autowired
	private IPropertyConstraintService propertyConstraintService;
	
	@RequiresPermissions("module:propertyConstraint:view")
	@GetMapping()
	public String propertyConstraint()
	{
	    return prefix + "/propertyConstraint";
	}
	
	/**
	 * 查询配置属性约束列表
	 */
	@RequiresPermissions("module:propertyConstraint:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(PropertyConstraint propertyConstraint)
	{
		startPage();
        List<PropertyConstraint> list = propertyConstraintService.selectPropertyConstraintList(propertyConstraint);
		return getDataTable(list);
	}
	
	/**
	 * 新增配置属性约束
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存配置属性约束
	 */
	@RequiresPermissions("module:propertyConstraint:add")
	@Log(title = "配置属性约束", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(PropertyConstraint propertyConstraint)
	{		
		return toAjax(propertyConstraintService.insertPropertyConstraint(propertyConstraint));
	}

	/**
	 * 修改配置属性约束
	 */
	@GetMapping("/edit/{configPropertyConstraintId}")
	public String edit(@PathVariable("configPropertyConstraintId") Integer configPropertyConstraintId, ModelMap mmap)
	{
		PropertyConstraint propertyConstraint = propertyConstraintService.selectPropertyConstraintById(configPropertyConstraintId);
		mmap.put("propertyConstraint", propertyConstraint);
		if(propertyConstraint.getBeanName()!=null){
			PropConstraintVO value = propConstraintScan.getPropConstraints().get(propertyConstraint.getBeanName());
			mmap.put("propertyConstraintBean", value);
		}else{
			mmap.put("propertyConstraintBean", null);
		}
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存配置属性约束
	 */
	@RequiresPermissions("module:propertyConstraint:edit")
	@Log(title = "配置属性约束", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(PropertyConstraint propertyConstraint)
	{

		return toAjax(propertyConstraintService.updatePropertyConstraint(propertyConstraint));
	}
	
	/**
	 * 删除配置属性约束
	 */
	@RequiresPermissions("module:propertyConstraint:remove")
	@Log(title = "配置属性约束", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(propertyConstraintService.deletePropertyConstraintByIds(ids));
	}

	/**
	 * 接口项目中选择域名
	 */
	@GetMapping("/lookupList/{uniqueId}")
	public String lookupList(@PathVariable("uniqueId") Integer uniqueId, ModelMap mmap)
	{
		if(uniqueId!=null){
			mmap.put("baseModel", propertyConstraintService.selectPropertyConstraintById(uniqueId));
		}
		return prefix + "/lookup";
	}

	/**
	 * 加载约束选择列表树
	 */
	@PostMapping("/lookupListData")
	@ResponseBody
	public TableDataInfo lookupListData(PropertyConstraint propertyConstraint)
	{
		startPage();
		List<PropertyConstraint> list = propertyConstraintService.selectPropertyConstraintList(propertyConstraint);
		return getDataTable(list);
	}
}
